//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;

namespace Eas.WebCrawler.PlugIns.LinkExtractors {
    /// <summary>Extracts links from html pages by using a collection of common regular expressions.</summary>
    public class HtmlLinkExtractor : Eas.WebCrawler.PlugIns.LinkExtractors.RegularExpressionLinkExtractor{
        private const string linkExpression = "(<a[^>]*href\\s*=\\s*[\\\"\\']?(?<LinkURL>[^\\\"'>\\s]*)[\\\"\\']?[^>]*>(?<Title>[^<]+|.*?)?</a\\s*>)|(\\\"\\s*(?<LinkURL>[^\\\"]+\\.(s?html?|gif|jpe?g|png|jsp|cfm|zip|exe|aspx?|pl|pdf|xml|ra|asx|ram|swf|php))\\s*\\\")|('\\s*(?<LinkURL>[^']+\\.(s?html?|gif|jpe?g|png|jsp|cfm|zip|exe|aspx?|pl|pdf|xml|ra|asx|ram|swf|php))\\s*')|(\\\"\\s*(?<LinkURL>https?://[^\"]+)\\\")|('\\s*(?<LinkURL>https?://[^']+)')";
        private static System.Text.RegularExpressions.Regex linkRegEx = new System.Text.RegularExpressions.Regex(
            linkExpression,
            System.Text.RegularExpressions.RegexOptions.Compiled |
            System.Text.RegularExpressions.RegexOptions.CultureInvariant |
            System.Text.RegularExpressions.RegexOptions.Multiline |
            System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        private const string linkExpressionGroupName = "LinkURL";

        /// <summary>Contstructs a <see cref="HtmlLinkExtractor"/>.</summary>
        public HtmlLinkExtractor()
            : base( new RegexSpecification( linkRegEx, linkExpressionGroupName) ) {
        }
    }
}
